package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql" // 不用，但是需要导入进来，mysql的驱动包
	"github.com/go-xorm/xorm"
	"log"
)

// 使用mysql
const DriverName = "mysql"
// 用户名：密码：tcp协议，访问ip和端口/ 数据库的名字？编码
const MasterDataSourceName = "root:123456@tcp(127.0.0.1:3306)/superstar?charset=utf8"


type UserInfo struct {
	Id			int        `xorm:"not null pk autoincrement"`
	name        string
	SysCreated  int
	SysUpdated  int
}


func newEngin() *xorm.Engine {
	engine, err := xorm.NewEngine(DriverName, MasterDataSourceName)
	if err != nil {
		log.Fatal(newEngin, err)
		return nil
	}

	// Debug模式。打印全部 SQL语句
	engine.ShowSQL(true)
	return engine
}


// 查找多行
func ormFindRows(engine *xorm.Engine) {
	//list := make([]UserInfo, 0)
	////list := make(map[int64]UserInfo, 1)   //也可以是 map结构
	////var users = make([]UserInfo, 0)
	//err := engine.Where("id=?", 0).
	//	Find(&list)
	////results := make([]UserInfo)
	//if err == nil {
	//	fmt.Println("123开始打印数据")
	//	fmt.Printf("%v\n", list)
	//} else {
	//	fmt.Println(123)
	//	log.Fatal(err)
	//}
	user := &UserInfo{Id:2}
	ok, err := engine.Get(user)
	if ok {
		fmt.Printf("%s\n", (*user).name)
	} else {
		fmt.Println("err", err)
	}
}



func main() {
	engine := newEngin()
	//fmt.Println(engine)
	//err := engine.Sync2(new(UserInfo))
	//if err != nil {
	//	panic(err)
	//}
	ormFindRows(engine)

}